package cn.iocoder.yudao.module.hs.service.materialmode;

import cn.hutool.core.util.ObjUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.hs.controller.admin.materialmode.vo.MaterialModePageReqVO;
import cn.iocoder.yudao.module.hs.controller.admin.materialmode.vo.MaterialModeSaveReqVO;
import cn.iocoder.yudao.module.hs.dal.dataobject.customer.CustomerDO;
import cn.iocoder.yudao.module.hs.dal.dataobject.material.MaterialDO;
import cn.iocoder.yudao.module.hs.dal.dataobject.materialmode.MaterialModeDO;
import cn.iocoder.yudao.module.hs.dal.dataobject.warehouse.WarehouseDO;
import cn.iocoder.yudao.module.hs.dal.mysql.materialmode.MaterialModeMapper;
import cn.iocoder.yudao.module.hs.enums.materialmode.MaterialModeConstants;
import cn.iocoder.yudao.module.hs.service.customer.CustomerService;
import cn.iocoder.yudao.module.hs.service.material.MaterialService;
import cn.iocoder.yudao.module.hs.service.warehouse.WarehouseService;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.hs.enums.ErrorCodeConstants.MATERIAL_MODE_NOT_EXISTS;

/**
 * 机型信息 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class MaterialModeServiceImpl implements MaterialModeService {

    @Resource
    private MaterialModeMapper materialModeMapper;
    @Resource
    private WarehouseService warehouseService;
    @Resource
    private CustomerService customerService;
    @Lazy
    @Resource
    private MaterialService materialService;
    @Resource
    private AdminUserApi adminUserApi;

    @Override
    public void createMaterialMode(MaterialModeSaveReqVO createReqVO) {
        MaterialModeDO materialMode = BeanUtils.toBean(createReqVO, MaterialModeDO.class);
        WarehouseDO productionWarehouse = warehouseService.getWarehouse(createReqVO.getProductionWarehouseId());
        materialMode.setProductionWarehouseName(productionWarehouse.getName());
        CustomerDO customer = customerService.getCustomer(createReqVO.getCustomerId());
        materialMode.setCustomerName(customer.getCustomerName());
        if (ObjUtil.isNotNull(materialMode.getTripartiteWarehouseId())) {
            WarehouseDO tripartiteWarehouse = warehouseService.getWarehouse(materialMode.getTripartiteWarehouseId());
            materialMode.setTripartiteWarehouseName(tripartiteWarehouse.getName());
            materialMode.setIsDirect(MaterialModeConstants.IS_DIRECT_NO);
        } else {
            materialMode.setIsDirect(MaterialModeConstants.IS_DIRECT_YES);
        }
        materialModeMapper.insert(materialMode);
    }

    @Override
    public void updateMaterialMode(MaterialModeSaveReqVO updateReqVO) {
        // 校验存在
        validateMaterialModeExists(updateReqVO.getId());
        LambdaUpdateWrapper<MaterialModeDO> updateWrapper = new LambdaUpdateWrapper<MaterialModeDO>();
        updateWrapper.set(MaterialModeDO::getModeCode, updateReqVO.getModeCode())
                .set(MaterialModeDO::getLandingQc, updateReqVO.getLandingQc())
                .set(MaterialModeDO::getPartNumber, updateReqVO.getPartNumber())
                .set(MaterialModeDO::getPartNumberShort, updateReqVO.getPartNumberShort())
                .set(MaterialModeDO::getBoxCapacityCount, updateReqVO.getBoxCapacityCount());
        WarehouseDO productionWarehouse = warehouseService.getWarehouse(updateReqVO.getProductionWarehouseId());
        updateWrapper.set(MaterialModeDO::getProductionWarehouseId, updateReqVO.getProductionWarehouseId());
        updateWrapper.set(MaterialModeDO::getProductionWarehouseName, productionWarehouse.getName());
        CustomerDO customer = customerService.getCustomer(updateReqVO.getCustomerId());
        updateWrapper.set(MaterialModeDO::getCustomerId, updateReqVO.getCustomerId());
        updateWrapper.set(MaterialModeDO::getCustomerName, customer.getCustomerName());
        if (ObjUtil.isNotNull(updateReqVO.getTripartiteWarehouseId())) {
            WarehouseDO tripartiteWarehouse = warehouseService.getWarehouse(updateReqVO.getTripartiteWarehouseId());
            updateWrapper.set(MaterialModeDO::getTripartiteWarehouseId, updateReqVO.getTripartiteWarehouseId());
            updateWrapper.set(MaterialModeDO::getTripartiteWarehouseName, tripartiteWarehouse.getName());
            updateWrapper.set(MaterialModeDO::getIsDirect, MaterialModeConstants.IS_DIRECT_NO);
        } else {
            updateWrapper.set(MaterialModeDO::getIsDirect, MaterialModeConstants.IS_DIRECT_YES);
        }
        updateWrapper.eq(MaterialModeDO::getId, updateReqVO.getId());
        materialModeMapper.update(updateWrapper);
    }

    @Override
    public void deleteMaterialMode(Long id) {
        // 校验存在
        validateMaterialModeExists(id);
        // 删除
        materialModeMapper.deleteById(id);
    }

    private void validateMaterialModeExists(Long id) {
        if (materialModeMapper.selectById(id) == null) {
            throw exception(MATERIAL_MODE_NOT_EXISTS);
        }
    }

    @Override
    public MaterialModeDO getMaterialMode(Long id) {
        return materialModeMapper.selectById(id);
    }

    @Override
    public PageResult<MaterialModeDO> getMaterialModePage(MaterialModePageReqVO pageReqVO) {
        return materialModeMapper.selectPage(pageReqVO);
    }

    @Override
    public MaterialModeDO getMaterialMode(String materialMode, Long warehouseId) {
        return materialModeMapper.selectFirstOne(MaterialModeDO::getModeCode, materialMode, MaterialModeDO::getTripartiteWarehouseId, warehouseId);
    }

    @Override
    public MaterialModeDO getMaterialMode(String materialMode, Integer isDirect) {
        return materialModeMapper.selectFirstOne(MaterialModeDO::getModeCode, materialMode, MaterialModeDO::getIsDirect, isDirect);
    }

    @Override
    public MaterialModeDO getMaterialMode(String materialMode) {
        return materialModeMapper.selectFirstOne(MaterialModeDO::getModeCode, materialMode);
    }

    @Override
    public MaterialModeDO getMaterialModeByMaterialCode(String materialCode) {
        AdminUserRespDTO user = adminUserApi.getUser(getLoginUserId());
        if (user == null || ObjUtil.isNull(user.getWarehouseId())) {
            throw exception(MATERIAL_MODE_NOT_EXISTS);
        }
        MaterialDO material = materialService.getMaterial(materialCode);
        return getMaterialMode(material.getModeCode(), user.getWarehouseId());
    }
}